package freebase;

import java.io.*;

public class HandleTSV2List {
	public static void main(String []args) throws Exception{
		String tsvfilelist = args[0];
		String dir = args[1];
		BufferedReader br_files = new BufferedReader(new FileReader(tsvfilelist));
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(args[2]),"utf-8");
		String line = "";
		int filen = 0;
		while((line = br_files.readLine())!=null){
			filen++;
			System.out.println("filen "+filen+"\t"+line);
			String file = line.replace("./", dir+"/");
			String filetype = line.replace("./", "").replace(".tsv", "");
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line2 = br.readLine();//this is metadata
			String []metadata = line2.split("\t");
			while((line2 = br.readLine())!=null){
				String []abc = line2.split("\t");
				String guid = abc[1].replace("/guid/", "");
				osw.write(guid+"\t"+filetype+"\t"+metadata[0]+"\t"+abc[0]+"\n");
				for(int i=2;i<metadata.length && i< abc.length;i++){
					String prop = metadata[i];
					String value = abc[i];
					if(value.length()>=1){
						osw.write(guid+"\t"+filetype+"\t"+prop+"\t"+value+"\n");
					}
				}
			}
			//check all metadata;
//			for(int i=0;i<metadata.length; i++){
//				osw.write(metadata[i]+"\t");
//			}
			
			//osw.write("\n");
//			while((line2 = br.readLine())!=null){
//				
//			}
			br.close();
			
		}
		osw.close();
		br_files.close();
	}
	
	public static void main3(String []args)throws Exception{
		BufferedReader br = new BufferedReader(new FileReader("O:\\unix\\projects\\pardosa\\data06\\clzhang\\freebase\\tsv2list"));
		String line = "";
		int []maxlen=new int[4];
		int ln =0;
		while((line = br.readLine())!=null){
			if(ln%10000==0)System.out.print(".");
			if(ln%1000000==0)System.out.println();
			ln++;
			String []abc = line.split("\t");
			for(int i=0;i<abc.length;i++){
				maxlen[i] = Math.max(maxlen[i], abc[i].length());
			}
		}
		for(int i=0;i<maxlen.length;i++){
			System.out.println(maxlen[i]);
		}
		
	}
	
	public static void main_3(String []args) throws Exception{
		String tsvfilelist = args[0];
		String dir = args[1];
		BufferedReader br_files = new BufferedReader(new FileReader(tsvfilelist));
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(args[2]),"utf-8");
		String line = "";
		int filen = 0;
		while((line = br_files.readLine())!=null){
			filen++;
			System.out.println("filen "+filen+"\t"+line);
			String file = line.replace("./", dir+"/");
			String filetype = line.replace("./", "").replace(".tsv", "");
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line2 = br.readLine();//this is metadata
			String []metadata = line2.split("\t");
			
			//check all metadata;
//			for(int i=0;i<metadata.length; i++){
//				osw.write(metadata[i]+"\t");
//			}
			
			//osw.write("\n");
//			while((line2 = br.readLine())!=null){
//				
//			}
			br.close();
			
		}
		osw.close();
		br_files.close();
	}
}
